// pages/login/login.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    password: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(wx.removeStorage);
    /* wx.login({
      success: res => {
        var code =res.code;
        if(code)
          console.log("获取的用户登录凭证："+ code);
        else
          console.log("获取的用户登录失败" + res.errMsg);  
      }
    })
    
    var app = getApp();
    app.globalData.userInfo = {
      username: "sss",
      password: "222"
    }
    console.log(app.globalData);
    */
    var that = this; //记录this

    //获取本地存储记录
    wx.getStorage({    
      key: 'login',
      success(res) {
        console.log(res.data)

        var user = res.data.username;
        var pwd = res.data.password;
        
        if (user && pwd) {
          /* wx.showToast({
            title: "登录成功",
            icon: 'none',
            duration: 1000,
          })  */
          that.setData({
            username: user,
            password: pwd
          })
          wx.showLoading({
            title: '登录成功',
          })
          setTimeout(function(){
            wx.hideLoading();
            /* wx.switchTab({
              url: '/pages/index/index'
            }) */
            wx.navigateTo({
              url: '/pages/entryWant/entryWant?id=5',
              success:function(){
                console.log("model1页面跳转成功");
              },
              fali:function(res){
                console.log("model1页面跳转失败")
              }
            })
          },500)
        }
      }
    })
    
    //获取登录信息 
   /*  var ss = wx.getStorageSync( 'login');
    console.log(ss); */
    //console.log(wx.getStorageSync('login'));
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function(e) {
    console.log("form发生了submit事件,携带数据为：",e.detail.value);
    var user = e.detail.value.username;
    var pwd = e.detail.value.password;
    if (user ==''){
      wx.showToast({
        title:"账号不能为空",
        icon:'none',
        duration: 1000,
      })
    } else if (pwd == ''){
      wx.showToast({
        title:'密码不能为空',
        icon:'none',
        duration: 1000,
      })
    }else{
      //post方式提交
      wx.request({
        url: "https://17945k20t1.iask.in:16458",  //https://17945k20t1.iask.in:16458  https://tcb-api.tencentcloudapi.com 
        method:"post",
        data:{
          username: user,
          password: pwd
        },
        header:{
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success:function(res){
          console.log(res);
          if (res.data.error == true) {
          //错误信息
            wx.showToast({
              title: res.data.errMsg,
              icon:'none',
              duration: 1000
            })
          }else{
            if (res.data.data){
              //提交成功，修改本地存储
              var _login = wx.getStorageSync('login');  //wx.getStorageSync
              _login.username = user;
              _login.password = pwd;
              console.log(_login);
              wx.setStorageSync("login", _login);  //wx.setStorageSync

              wx.showToast({
                title:res.data.msg,
                icon:'none',
                duration: 1000,
                success:function(){
                  setTimeout(function(){
                    wx.relaunch({
                      url:'',
                    })
                  },2000)
                }
              })
            }  
          }
        },
        error:function(){
          alert();
        }
      })
    }
  },
  formReset:function(){
    console.log("form发生了reset事件")
  }
})